const app = getApp()
Component({
    options: {
        multipleSlots: true // 在组件定义时的选项中启用多slot支持
    },
    /**
     * 组件的属性列表
     */
    properties: {
        title: { // 属性名
            type: String, // 类型（必填），目前接受的类型包括：String, Number, Boolean, Object, Array, null（表示任意类型）
            value: '标题' // 属性初始值（可选），如果未指定则会根据类型选择一个
        },
        // 弹窗内容
        content: {
            type: String,
            value: '内容'
        },
        // 弹窗取消按钮文字
        btn_no: {
            type: String,
            value: '取消'
        },
        // 弹窗确认按钮文字
        btn_ok: {
            type: String,
            value: '确定'
        },
        course: {
            type: Object,
            value: null
        }
    },
    observers: {
        'flag'(data) {
            if (!data) {
                this.data.halfList.forEach(item => {
                    item.isSelected = false
                })
                this.setData({ halfList: this.data.halfList })
            }
        },
        'course'(data) {
            if (data) {
                const half = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']
                const two_half = ['OUT', 'IN']
                const halfList = []
                const hole = data.course_hole || 0
                if (hole % 9 === 0 && hole > 17) {
                    const groupNum = parseInt(hole / 9)
                    if (groupNum == 2) {
                        for (let i = 0; i < groupNum; i++) {
                            const obj = {
                                name: `${data.course_name}（${two_half[i]}场）9洞`,
                                half: two_half[i],
                                isSelected: false
                            }
                            halfList.push(obj)
                        }
                    } else {
                        for (let i = 0; i < groupNum; i++) {
                            const obj = {
                                name: `${data.course_name}（${half[i]}场）9洞`,
                                half: half[i],
                                isSelected: false
                            }
                            halfList.push(obj)
                        }
                    }
                }
                this.setData({ halfList })
            }
        }
    },
    /**
     * 组件的初始数据
     */
    data: {
        flag: true,
        halfList: [],
        staticUrl: app.globalData.staticUrl,
        checkedHalf: [],
        halfText: '',
        courseHole: []
    },

    /**
     * 组件的方法列表
     */
    methods: {
        bindCheckChange(e) {
            const { index } = e.currentTarget.dataset
            let { halfList, checkedHalf, halfText, courseHole } = this.data
            halfList[index].isSelected = !halfList[index].isSelected
            const checkedList = halfList.filter(item => item.isSelected === true)
            if (checkedList.length > 2) {
                halfList[index].isSelected = false
            }
            // 半场顺序
            halfList.forEach(item => {
                const i = checkedHalf.findIndex(e => e.indexOf(item.half) > -1)
                const obj = `${item.half}场9洞`
                if (item.isSelected) {
                    if (i == -1) { //不包含
                        checkedHalf.push(obj)
                        courseHole.push(item.half)
                    }
                } else {
                    if (i > -1) { //包含
                        checkedHalf.splice(i, 1)
                        courseHole.splice(i, 1)
                    }
                }
            })
            halfText = checkedHalf.join('；')
            this.setData({ halfList, checkedHalf, halfText, courseHole })
        },
        //隐藏弹框
        hidePopup: function() {
            this.setData({
                flag: !this.data.flag
            })
        },
        //展示弹框
        showPopup() {
            this.setData({
                flag: !this.data.flag,
                checkedHalf: [],
                halfText: ''
            })
        },
        /*
         * 内部私有方法建议以下划线开头
         * triggerEvent 用于触发事件
         */
        _error() {
            //触发取消回调
            this.triggerEvent("error")
        },
        _success() {
            if (this.data.courseHole.length !== 2) {
                wx.showToast({
                    title: '请选择前后九场地',
                    icon: 'none'
                })
                return
            }
            wx.hideToast({})
            const course = Object.assign(this.properties.course, { cr_hole: this.data.courseHole.join(',') })
            //触发成功回调
            this.triggerEvent("success", course);
        }
    }
})